package org.jeecg.modules.tls.oes.paper.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import org.jeecg.modules.tls.oes.paper.entity.OesPaper;
import org.jeecg.modules.tls.oes.paper.entity.paper.PaperItemObject;
import org.jeecg.modules.tls.oes.paper.entity.paper.PaperObject;
import org.jeecg.modules.tls.oes.paper.mapper.OesPaperMapper;
import org.jeecg.modules.tls.oes.paper.service.IOesPaperService;
import org.jeecg.modules.tls.oes.question.entity.OesQuestion;
import org.jeecg.modules.tls.oes.question.mapper.OesQuestionMapper;
import org.jeecg.modules.tls.oes.question.service.IOesQuestionService;
import org.jeecg.modules.tls.oes.textcontent.entity.OesTextContent;
import org.jeecg.modules.tls.oes.textcontent.mapper.OesTextContentMapper;
import org.jeecg.modules.tls.oes.utils.JsonUtil;
import org.jeecg.modules.tls.oes.viewmodel.admin.paper.ExamPaperEditRequestVM;
import org.jeecg.modules.tls.oes.viewmodel.admin.paper.ExamPaperTitleItemVM;
import org.jeecg.modules.tls.oes.viewmodel.student.paper.PaperRequestVM;
import org.jeecg.modules.tls.oes.viewmodel.student.paper.ExamPaperTitleItemRequestVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @Description: oes_paper
 * @Author: jeecg-boot
 * @Date:   2022-07-18
 * @Version: V1.0
 */
@Service
public class OesPaperServiceImpl extends ServiceImpl<OesPaperMapper, OesPaper> implements IOesPaperService {
    @Autowired
    OesPaperMapper oesPaperMapper;
    @Autowired
    OesTextContentMapper oesTextContentMapper;
    @Autowired
    OesQuestionMapper oesQuestionMapper;
    @Autowired
    IOesQuestionService iOesQuestionService;

    //试卷总分计算
    Integer score = 0;
    @Override
    public PaperRequestVM getExamPaperRequestVM(String id) {
        //通过id查试卷信息
        OesPaper oesPaper = oesPaperMapper.selectById(id);
        PaperRequestVM paperRequestVM = new PaperRequestVM();

        //设置基础信息
        paperRequestVM.setId(oesPaper.getId());
        paperRequestVM.setName(oesPaper.getName());
        paperRequestVM.setScore(oesPaper.getScore());

        //查对应的试卷结构
        OesTextContent textContent = oesTextContentMapper.selectById(oesPaper.getFrameTextContentId());

        //查询试卷结构对应的题目
        List<ExamPaperTitleItemVM> examPaperTitleItemVMS = JSONUtil.toList(textContent.getContent(), ExamPaperTitleItemVM.class);
        List<ExamPaperTitleItemRequestVM> titleItems = examPaperTitleItemVMS.stream().map(i->{
            ExamPaperTitleItemRequestVM examPaperTitleItemRequestVM = new ExamPaperTitleItemRequestVM();
            examPaperTitleItemRequestVM.setName(i.getName());
            examPaperTitleItemRequestVM.setQuestionItems(iOesQuestionService.getQuestionItems(i.getQuestionItems()));
            return examPaperTitleItemRequestVM;
        }).collect(Collectors.toList());
        paperRequestVM.setItems(titleItems);
        return paperRequestVM;
    }

    @Override
    public void addPaper(ExamPaperEditRequestVM model) {
        //当前日期
        Date date = DateUtil.date();
        //试卷类和文本类
        OesPaper oesPaper = new OesPaper();
        OesTextContent oesTextContent = new OesTextContent();

        oesTextContent.setCreateTime(date);
        //自增序列，用于排序
        AtomicInteger index = new AtomicInteger(1);
        //设置题目结构 标题和题目id
        List<PaperObject> paperObjectList = model.getItems().stream().map(i->{
            PaperObject paperObject = new PaperObject();
            paperObject.setName(i.getName());
            List<PaperItemObject> paperItemObjectList = i.getQuestionItems().stream().map(j->{
                PaperItemObject paperItemObject = new PaperItemObject();
                paperItemObject.setId(j.getId());
                paperItemObject.setItemOrder(index.getAndIncrement());
                OesQuestion question = oesQuestionMapper.selectById(j.getId());
                score=score+question.getScore();
                return paperItemObject;
            }).collect(Collectors.toList());
            paperObject.setQuestionItems(paperItemObjectList);
            return paperObject;
        }).collect(Collectors.toList());
        //存储为json格式
        oesTextContent.setContent(JsonUtil.toJsonStr(paperObjectList));
        oesTextContentMapper.insert(oesTextContent);

        oesPaper.setFrameTextContentId(oesTextContent.getId());
        oesPaper.setName(model.getName());
        oesPaper.setScore(score);
        oesPaper.setDeleted(0);
        oesPaper.setCreateTime(date);
        oesPaper.setCreateUserId(model.getUserId());
        oesPaperMapper.insert(oesPaper);
        score = 0;

    }

    @Override
    public void updatePaper(ExamPaperEditRequestVM model) {
        OesPaper oesPapered = oesPaperMapper.selectById(model.getId());
        PaperRequestVM paperRequestVM = new PaperRequestVM();

        //当前日期
        Date date = DateUtil.date();
        //试卷类和文本类
        OesPaper oesPaper = new OesPaper();
        //自增序列，用于排序
        AtomicInteger index = new AtomicInteger(1);
        OesTextContent oesTextContent = new OesTextContent();
        oesTextContent.setId(oesPapered.getFrameTextContentId());
        List<PaperObject> paperObjectList = model.getItems().stream().map(i->{
            PaperObject paperObject = new PaperObject();
            paperObject.setName(i.getName());
            List<PaperItemObject> paperItemObjectList = i.getQuestionItems().stream().map(j->{
                PaperItemObject paperItemObject = new PaperItemObject();
                paperItemObject.setId(j.getId());
                paperItemObject.setItemOrder(index.getAndIncrement());
                OesQuestion question = oesQuestionMapper.selectById(j.getId());
                score=score+question.getScore();
                return paperItemObject;
            }).collect(Collectors.toList());
            paperObject.setQuestionItems(paperItemObjectList);
            return paperObject;
        }).collect(Collectors.toList());
        oesTextContent.setContent(JsonUtil.toJsonStr(paperObjectList));
        oesTextContentMapper.updateById(oesTextContent);

        oesPaper.setId(model.getId());
        oesPaper.setName(model.getName());
        oesPaper.setScore(score);
        oesPaperMapper.updateById(oesPaper);
        score = 0;
    }
}




















